# -*- encoding: utf-8 -*-
"""
@File    : getTxt.py
@Time    : 2021/11/17 11:16
@Author  : fkl
@Software: PyCharm
"""

import requests
from classes.Config import Config
from classes.Common import Common
from classes.Thread import MyThread
import parsel
import os


search = input("输入小说名称：")
Config = Config()
URL_SEARCH = Config.get("URL_SEARCH") + search
headers = {
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.53"
}
response = requests.get(URL_SEARCH, headers)
selector = parsel.Selector(text=response.text)
arrHref = selector.xpath('//div[@class="bookbox"]//h4[@class="bookname"]/a/@href').getall()
if len(arrHref) <= 0:
    print("未找到")
    exit()
arrName = selector.xpath('//div[@class="bookbox"]//h4[@class="bookname"]/a/text()').getall()
for k, v in enumerate(arrName):
    print(str(k) + '：' + v)
num = int(input("请输入编号："))
URL = ''
try:
    URL = Config.get("URL") + arrHref[num]
except Exception as e:
    print("编号输入有误！")
    exit()
print("正在下载，请稍候...")
response2 = requests.get(URL, headers)
selector2 = parsel.Selector(text=response2.text)
head = selector2.xpath('//div[@id="book"]//div[@id="info"]')
name = head.xpath("//h1/text()").get()
brief = head.xpath("//p/text()").getall()

txtDir = os.path.join(Config.get("P_PATH"), 'txt', name + ".txt")
txt = open(txtDir, 'w' ,encoding='utf-8')
txt.write(str(name) + "\n")
for k, v in enumerate(brief):
    if k <= 2:
        txt.write(v + "\n")

lists = selector2.xpath('//div[@class="listmain"]//dd/a/@href').getall()

pageSize = int(Config.get("SIZE"))
lists2 = Common.list_split(lists, pageSize)
success_num = 0

for val in lists2:
    conObj = {}
    threads = []
    for k, v in enumerate(val):
        url = Config.get("URL") + v
        threads.append(MyThread(url, headers))
    for v in threads:
        v.start()
    for v in threads:
        v.join()
    for k, v in enumerate(threads):
        if v.status == 1:
            conObj[k] = {
                'title': v.title,
                'content': v.content,
            }
            success_num += 1
    for v in conObj.values():
        txt.write("\n\n\n" + v['title'] + "\n\n\n")
        txt.write(v['content'] + "\n")


txt.close()
print('已完成, 共' + str(success_num) + "页")



